import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.Utils import ExamDriver

#测试考试过程保存
class ExamSaving:
    url = ""
    driver = ""
    def __init__(self):
        self.driver = ExamDriver.driver
        self.url = "https://gms.test.tgtrain.cn/exam/detail?id=19016&did=29884"
        self.driver.get(self.url)

    #已登录
    def Auto_save(self):
        # 1.自动保存成功
        # 点击开始考试
        self.driver.find_element(By.CSS_SELECTOR, "#app > section > section.mod-1 > div.content > div.bottom > div.btn > button").click()
        self.driver.implicitly_wait(5)
        #执行前要点击确定的弹窗
        #添加显示等待
        element = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div > div > div.ant - modal - wrap > div > div.ant - modal - content > div > div > div.ant - modal - confirm - btns > button")))
        element.click()

        #30秒自动保存
        #点击A
        self.driver.find_element(By.CSS_SELECTOR, "#que_56938 > div:nth-child(3) > div > label:nth-child(1) > span:nth-child(2) > div > div.letter").click()
        time.sleep(30)

        self.driver.refresh()

        self.driver.find_element(By.CSS_SELECTOR,
                                 "div > div > div.ant - modal - wrap > div > div.ant - modal - content > div > div > div.ant - modal - confirm - btns > button").click()
        #取答题进度的数字，即‘1/5’就是保存成功，‘0/5’就是保存失败
        progress = self.driver.find_element(By.CSS_SELECTOR, "#app > div > div > section.main > div > div.sidebar > div > div.sheet > div.progress > span").text

        #添加截图
        ExamDriver.getScreeShot()
        assert progress == '1/5'

        # 2.自动保存失败（29秒）
        self.driver.find_element(By.CSS_SELECTOR, "#que_74355 > div:nth-child(3) > div > label:nth-child(1) > span:nth-child(2) > div > div.letter").click()
        time.sleep(10)

        self.driver.refresh()

        element = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,
                                                                                   "div > div > div.ant - modal - wrap > div > div.ant - modal - content > div > div > div.ant - modal - confirm - btns > button")))
        element.click()
        # 取答题进度的数字，即‘2/5’就是保存成功，‘1/5’就是保存失败
        progress = self.driver.find_element(By.CSS_SELECTOR, "#app > div > div > section.main > div > div.sidebar > div > div.sheet > div.progress > span").text
        # 添加截图
        ExamDriver.getScreeShot()
        assert progress == '1/5'

        #3.考试结束自动提交
        self.driver.find_element(By.CSS_SELECTOR, "#que_74355 > div:nth-child(3) > div > label:nth-child(1) > span:nth-child(2) > div > div.letter").click()

        time.sleep(20)
        self.driver.find_element(By.CSS_SELECTOR, "app > section > section.mod-1 > div.content > div.top > div.lable > div > h1 > span")

        #4.手动保存
        #在30秒内答题后手动点击“暂存”，再次刷新若进度保存即手动保存成功
        # 点击开始考试
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > section > section.mod-1 > div.content > div.bottom > div.btn > button").click()
        self.driver.implicitly_wait(5)
        # 执行前要点击确定的弹窗
        element = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div > div > div.ant - modal - wrap > div > div.ant - modal - content > div > div > div.ant - modal - confirm - btns > button")))
        element.click()
        # 点击A
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#que_56938 > div:nth-child(3) > div > label:nth-child(1) > span:nth-child(2) > div > div.letter").click()

        #点击暂存
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > div > section.main > div > div.sidebar > div > div.sheet > div.btn > button.ant-btn.ant-btn-primary.ant-btn-dangerous").click()

        #添加显示等待
        element = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "body > div:nth-child(10) > div > div.ant-modal-wrap > div > div.ant-modal-content > div > div > div.ant-modal-confirm-btns > button.ant-btn.ant-btn-primary")))
        element.click()

        #点击刷新
        self.driver.refresh()

        # 取答题进度的数字，即‘1/5’就是保存成功，‘0/5’就是保存失败
        time.sleep(3)
        progress = self.driver.find_element(By.CSS_SELECTOR, "#app > div > div > section.main > div > div.sidebar > div > div.sheet > div.progress > span").text
        # 添加截图
        ExamDriver.getScreeShot()
        assert progress == '1/5'